Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
The Text Box control’s KeyPress event procedure links the user’s input to the Comm control. Rather than sending each individual character as it is typed, this procedure saves the input in a buffer and sends it all at once when the user presses the Enter key. Received data is handled in the OnComm event procedure. After checking the CommEvent property to see that the event was indeed triggered by receipt of data, the code adds the received character to the end of the Text Box’s Text property, then moves the insertion point to the end of the text. Figure 15.1 Executing the “dumb terminal” program. Listing 15.2 Code in COMMDEMO.FRM.
    Option Explicit

    Dim Buf1 As String

    Private Sub Text1_KeyPress(KeyAscii As Integer)

    ‘ Add the key to the buffer.
    Buf1 = Buf1 & Chr$(KeyAscii)

    ‘ If it was a carriage return, send the buffer contents
    ‘ to the Comm control and empty the buffer.
    If KeyAscii = 13 Then
        MSComm1.Output = Buf1
        Buf1 = “”
    End If

    End Sub

    Private Sub Form_Load()

    ‘ Initialize the Comm control.
    MSComm1.CommPort = 1
    MSComm1.Settings = “14000,E,7,1”
    Debug.Print “----------------”

    ‘ Comm event will be triggered when a single character is received.
    MSComm1.RThreshold = 1

    ‘ Open the port.
    MSComm1.PortOpen = True

    End Sub

    Private Sub Form_Resize()

    ‘ Set Text Box to fill the form.
    Text1.Width = Form1.ScaleWidth
    Text1.Height = Form1.ScaleHeight
    Text1.Left = 0
    Text1.Top = 0

    End Sub

    Private Sub Form_Unload(Cancel As Integer)

    ‘ Close the port.
    MSComm1.PortOpen = False

    End Sub

    Private Sub MSComm1_OnComm()

    Dim s As String

    ‘ When a comm event occurs.

    ‘ Was it a “receive” event? If so, add the received character
    ‘ to the Text Box and set the insertion point at the end of
    ‘ the text. Other events are ignored.

    Select Case MSComm1.CommEvent
        Case comEvReceive
            s = MSComm1.Input
            If Asc(s) = 13 Then s = vbCrLf
            Text1.Text = Text1.Text & s
            Text1.SelStart = Len(Text1.Text)
    End Select

    End Sub
If you use this program with a modem, you’ll have to type the modem commands yourself (the commands are usually sent automatically by a communication program). Most modems adhere to the AT command set, originally developed by Hayes for its modems. The first command you should send is ATZ, which resets and initializes the modem. The model will respond OK. A few other commands are listed in Table 15.3; refer to your modem documentation for more information. This very basic demonstration program is intended to show you only the fundamentals of using the Comm control. As it stands, the program includes no error handling and is vulnerable to many problems in use. Even with the Comm control, writing a full-featured and reliable serial communication program is a major task. Whenever I need anything but the most basic communication capabilities in a Visual Basic program, I always investigate the third-party market for a custom control rather than trying to do the job myself using the Comm control.
Table 15.3 Modem commands.
Command | Description
--- | ---
ATDT | Dial the following number using tone dialing. For example, ATDT5551212 would cause the modem to dial 555-1212.
ATE | Disable echoing of input. Use this command if the program is displaying everything you type twice.
ATH | Hang up the phone.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.